package com.binguo.api.controller;

import com.binguo.api.result.Result;
import com.binguo.api.result.ResultGenerator;
import com.binguo.api.service.RedisLockService;
import com.binguo.api.webapi_tts.WebTTs;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.UUID;

@Api(tags = "收银端: 文字转语音")
@RestController
@RequestMapping
public class AudioController {

    @Resource
    RedisLockService redisLockService;

    @GetMapping(value = "/api/getAudio")
    @ApiOperation(value = "获取音频url", notes = "")
    public Result getAudio(@RequestParam(value = "text", required = true) String text) {

        String uuid = UUID.randomUUID().toString();
        boolean lock = redisLockService.setLock("test", 10000, uuid);

        System.out.println(lock);

        boolean lock2 = redisLockService.setLock("test", 10000, uuid);
        System.out.println(lock2);

        if (lock) {
            redisLockService.releaseLock("test", uuid);
            boolean lock3 = redisLockService.setLock("test", 10000, uuid);

            System.out.println(lock3);
        }

        System.out.println(text);
        try {
            String key = UUID.randomUUID().toString();
            byte[] content = WebTTs.generate(text, key);

//            String key = UUID.randomUUID() + ".wav";

//            if(upload(content, key)){
            return ResultGenerator.genSuccessResult("https://binguoai.com/" + key + ".wav");
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResultGenerator.genFailResult("error");
    }


    @GetMapping(value = "/api/getAudioMp3")
    @ApiOperation(value = "获取音频url", notes = "")
    public Result getAudioMp3(@RequestParam(value = "text", required = true) String text) {


        System.out.println(text);
        try {
            String key = UUID.randomUUID().toString();
            byte[] content = WebTTs.generateMP3(text, key);

            return ResultGenerator.genSuccessResult("https://binguoai.com/" + key + ".mp3");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResultGenerator.genFailResult("error");
    }

    public static void main(String[] args) {
        String text = "欢迎光临";
        try {
            byte[] content = WebTTs.generate2(text);

            String key = UUID.randomUUID() + ".mp3";

//            if(upload(content, key)){
//
//                System.out.println("http://img.binguoai.com/" + key);
//
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


//    public static void main(String[] args) throws Exception {
//        System.out.println(AudioController.class.getResource("/").getPath());
//        System.out.println(ClassLoader.getSystemResource("").getPath());
//    }
}
